package com.jcl.recruit.basic.login.impl;

import com.jcl.core.utils.CollectionUtils;
import com.jcl.core.web.utils.SecurityUtils;
import com.jcl.recruit.basic.login.RecruitLogin;
import com.jcl.recruit.basic.login.model.LoginResult;
import com.jcl.recruit.basic.login.model.LoginResultContent;
import com.jcl.recruit.console.ConsoleCorpDao;
import com.jcl.recruit.console.ConsoleUserAccessDao;
import com.jcl.recruit.console.ConsoleUserTokenDao;
import com.jcl.recruit.console.model.ConsoleCorpInfo;
import com.jcl.recruit.console.model.ConsoleUserAccess;
import com.jcl.recruit.console.model.ConsoleUserToken;
import com.jcl.recruit.core.constants.UserExternalLoginConstant;
import com.jcl.recruit.user.dao.CorpUserDao;
import com.jcl.recruit.user.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @ClassName RecruitLoginImpl
 * @Author yyliu
 * @Date 2018/12/17 16:24
 * @Version 1.0
 **/
@Component("recruitLogin")
public class RecruitLoginImpl implements RecruitLogin {

    @Autowired
    private ConsoleCorpDao consoleCorpDao;

    @Autowired
    private ConsoleUserAccessDao consoleUserAccessDao;

    @Autowired
    private CorpUserDao corpUserDao;

    @Autowired
    private ConsoleUserTokenDao consoleUserTokenDao;

    @Override
    public LoginResult doLogin(String username,
                               String password,
                               String category) {
        LoginResult result = null;
        ConsoleUserAccess userAccess = consoleUserAccessDao.findOneByUsernameAndPw(username, password,
                                    UserExternalLoginConstant.USER_ACCESS_CATEGORY_NORMAL,
                                    UserExternalLoginConstant.USER_ACCESS_STATUS_USED);
        if(null == userAccess){
            result = new LoginResult();
            result.setCode(LoginResultContent.ACCOUNT_ERROR_CODE);
            result.setMsg(LoginResultContent.getMsg(LoginResultContent.ACCOUNT_ERROR_CODE));
            return result;
        }
        result = doLogin(userAccess.getCorpCode(), userAccess.getUserId(), category);
        return result;
    }

    @Override
    public LoginResult loginByExternalKey(String externalKey, String category) {

        // 查询正在使用的 账号
        List<ConsoleUserAccess> accessList = consoleUserAccessDao.findListByExternalKey(
                externalKey, category, UserExternalLoginConstant.USER_ACCESS_STATUS_USED);
        if(CollectionUtils.isNotEmpty(accessList)){
            ConsoleUserAccess access = accessList.get(0);
            if(null == access || null == access.getCorpCode() || null == access.getUserId()){
                LoginResult result = new LoginResult();
                result.setCode(LoginResultContent.ACCOUNT_ERROR_CODE);
                result.setMsg(LoginResultContent.getMsg(LoginResultContent.ACCOUNT_ERROR_CODE));
                return result;
            }
            return doLogin(access.getCorpCode(), access.getUserId(), category);
        }
        return null;
    }

    @Override
    public LoginResult wechatLogin(String corpCode, Integer userId) {
        LoginResult result = doLogin(corpCode, userId, SecurityUtils.FROM_WECHAT);
        return result;
    }

    public LoginResult doLogin(String corpCode, Integer userId, String fromType){
        LoginResult result = new LoginResult();

        User user = corpUserDao.findOne(corpCode, userId);
        if(null == user){
            result.setCode(LoginResultContent.ACCOUNT_ERROR_CODE);
            result.setMsg(LoginResultContent.getMsg(LoginResultContent.ACCOUNT_ERROR_CODE));
            return result;
        }else{
            ConsoleCorpInfo corpInfo = consoleCorpDao.findOneByCode(corpCode);
            Date abateDate = corpInfo.getAbateDate();
            String token = SecurityUtils.encodeUserToken(corpCode,
                    user.getId().toString(), user.getRoles(), abateDate, fromType);

            this.refreshUserToken(corpCode, userId, token);

            result.setCode(LoginResultContent.SUCCESS_CODE);
            result.setMsg(LoginResultContent.getMsg(LoginResultContent.SUCCESS_CODE));
            result.setUserStatus(user.getStatus());
        }
        return result;
    }

    private void refreshUserToken(String corpCode, Integer userId, String token){

        ConsoleUserToken userToken = consoleUserTokenDao.findOne(corpCode, userId);
        if(null == userToken){
            userToken = new ConsoleUserToken();
            userToken.setCorpCode(corpCode);
            userToken.setUserId(userId);
            userToken.setToken(token);
            Date date = new Date();
            userToken.setCreateTime(date);
            userToken.setUpdateTime(date);
            consoleUserTokenDao.add(userToken);

        }else{
            userToken.setToken(token);
            Date date = new Date();
            userToken.setCreateTime(date);
            userToken.setUpdateTime(date);
            consoleUserTokenDao.update(userToken);
        }
    }
}
